package com.sdry.web.controller.jyy;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sdry.model.jyy.Dispatch;
import com.sdry.model.jyy.DispatchExample;
import com.sdry.model.jyy.DispatchExample.Criteria;
import com.sdry.model.llm.Department;
import com.sdry.model.llm.User;
import com.sdry.service.jyy.DispatchServise;
import com.sdry.utils.DateUtil;
import com.sdry.utils.ResponseUtil;

@Controller
@RequestMapping("/dispatch")
public class DispatchController {
	
	@Resource
	private DispatchServise dispatchServise;
	

	/**
	 * 跳转到工作派遣单页面
	 * @return
	 */
	@RequestMapping("/dispatch")
	public String dispatchList(){
		return "/jyy/dispatch";
	}
	
	/**
	 * @param dispatch 工作派遣单实体
	 * @param response 响应
	 * @param limit 每页显示记录数
	 * @param page 页码
	 * @throws Exception 异常
	 */
	@RequestMapping("/selectList")
	public void selectList(Dispatch dispatch, HttpServletResponse response,
			@RequestParam(value = "limit",required = false)int limit,
			@RequestParam(value = "page",required = false)int page) throws Exception {
		
		PageHelper.startPage(page, limit);
		DispatchExample example = new DispatchExample();
		Criteria createCriteria = example.createCriteria();
		
		if(dispatch.getPeople() != null && dispatch.getPeople() != "") {
			createCriteria.andPeopleLike("%"+dispatch.getPeople()+"%");
		}
		List<Dispatch> list = dispatchServise.selectByExample(example );
		
		PageInfo<Dispatch> info = new PageInfo<Dispatch>(list);
    	
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("msg", "");
        jsonObject.put("count", info.getTotal());
        jsonObject.put("data", info.getList());
        ResponseUtil.write(response, jsonObject);
		
	}
	
	/**
	 * 添加工作派遣单
	 * @param dispatch 派遣单实体
	 * @param response 响应
	 * @throws Exception 异常
	 */
	@RequestMapping("/create")
	public void create(Dispatch dispatch, HttpServletResponse response) throws Exception {
		
		String format = DateUtil.dateFormat1();
		
		dispatch.setDatetime(format);
		//默认设置为未确认
		dispatch.setConfirm("未确认");
		
		int insert = dispatchServise.insert(dispatch);
		ResponseUtil.write(response, insert);
	}
	
	/**
	 * 批量删除工作派遣单
	 * @param ids 
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/delete")
	public void delete(String ids, HttpServletResponse response) throws Exception {
		int affact = dispatchServise.deleteByPrimaryKeys(ids);
		ResponseUtil.write(response, affact);
	}
	
	/**
	 * 动态修改工作派遣单
	 * @param dispatch
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/edit")
	public void edit(Dispatch dispatch, HttpServletResponse response) throws Exception {
		int affact = dispatchServise.updateByPrimaryKeySelective(dispatch);
		ResponseUtil.write(response, affact);
	}
	
	/**
	 * 查询所有部门
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping("/queryDepartment")
	public void queryDepartment(HttpServletResponse response) throws Exception {
		List<Department> list= dispatchServise.selectDepartment();
		
		//将对象转化为json
        JSONArray json = JSONArray.fromObject(list);
        ResponseUtil.write(response, json);
	}
	
	/**
	 * 查询所有员工
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping("/queryUser")
	public void queryUser(HttpServletResponse response) throws Exception {
		List<User> list= dispatchServise.selectUser();
		
		//将对象转化为json
		JSONArray json = JSONArray.fromObject(list);
		ResponseUtil.write(response, json);
	}
	
	@RequestMapping("/queryDepartmentLeder")
	public void queryDepartmentLeder(String name, HttpServletResponse response) throws Exception {
		String leader = "";
		String leader2 = dispatchServise.queryDepartmentLeder(name);
		
		if(leader2 != null && leader2 !="") {
			leader = leader2;
		}
		ResponseUtil.write(response, leader);
	}
	
	
	@RequestMapping("/confirm")
	public void confirm(Long id, HttpServletResponse response, HttpServletRequest request) throws Exception {
		
		int affact = 0;
		//先根据id查询是否已确认
		Dispatch dispatch = dispatchServise.selectByPrimaryKey(id);
		
		if(dispatch != null) {
			if(dispatch.getConfirm() != null && dispatch.getConfirm() != "") {
				
				String confirm = dispatch.getConfirm();
				if("未确认".endsWith(confirm)) {
					//未确认
					//获取session中的人名
					User people = (User) request.getSession().getAttribute("currentUser");
					//获取当前时间
					String format2 = DateUtil.dateFormat1();
					
					dispatch.setRemark(people.getUserName());
					dispatch.setRemark2(format2);
					//设置为已确认
					dispatch.setConfirm("已确认");
					
					affact = dispatchServise.updateByPrimaryKeySelective(dispatch);
				}else {
					
					//已确认
					affact = -1;
				}
			}
		}
		
		ResponseUtil.write(response, affact);
	}
	
}
